#include "./room.h"

sRoom*_room = NULL;

//创建room
sRoom*sNewRoom(int w,int h,int sp)
{
 sRoom*rm = malloc(sizeof(sRoom));
 rm->sen =
 malloc(sizeof(sObjSen));
 rm->sen->obj=NULL;
 rm->sen->next=NULL;
 rm->sp = sp;
 rm->tim = getuptime();
 rm->w = w;
 rm->h = h;
 rm->create = NULL;
 rm->draw = NULL;
 rm->exit = NULL;
 rm->event = NULL;
 rm->cam.x=0;
 rm->cam.y=0;
 rm->cam.w=SW;
 rm->cam.h=SH;
 setscrsize(w,h);
 printf("sgame>newRoom(%d,%d)\n",
 w,h);
 return rm;
}

//跳转房间
void sGotoRoom(sRoom*rm)
{
 _room = rm;
 //执行跳转
 printf("sgame>gotoRoom\n");
 if(_room->create!=NULL)
 {
  printf("sgame>call roomCreate\n");
  	_room->create(_room);
 }
}

//添加obj
void sRoomAddObj(sRoom*rm,sObj*obj)
{
 if(rm==NULL)
 return;
 sSenAddObj(rm->sen,obj);
}

void sDrawRoom(sRoom*rm)
{
 if(rm==NULL)
 return;
 if(rm->draw!=NULL)
  rm->draw(rm);
 else
  sDrawSenObjCam(rm->sen,
  &(rm->cam));
}

void sDrawCurRoom()
{
 sDrawRoom(_room);
}

//运行房间
void sRunRoom(sRoom*rm)
{
 if(rm==NULL)
 return;
 if(getuptime()-rm->tim>=rm->sp)
 {
  rm->tim=getuptime();
  sRunSenObj(rm->sen);
  //sCollSenAllObj(rm->sen);
 }
}

//运行当前房间
void sRunCurRoom()
{
 sRunRoom(_room);
}


//退出房间
void sExitRoom(sRoom*rm)
{
 printf("sgame>exitRoom\n");
 if(rm==NULL)
 return;
 if(rm->exit!=NULL)
 {
  printf("sgame>call exitRoom\n");
 	rm->exit(rm);
 }
 else
 {
  printf("sgame>def exitRoom\n");
  sDelSenAll(rm->sen);
 }
}



